import { Toast } from 'react-vant'
import { ETH } from "@tools/contract"

const fetchSign = async () => {
  let signStorage = localStorage.getItem('sign')
  const account = await ETH.getAccount();
  const accountLac = localStorage.getItem("account");

  if (account !== accountLac) {
    localStorage.removeItem('sign')
    signStorage = null
  }

  if (!signStorage) {
    try {
      const sign = await ETH.signMessage();
      localStorage.setItem('sign', sign)
      return sign
    } catch (error) {
      Toast.info('签名失败'); // 显示失败的提示信息
      throw '签名失败' // 抛出错误信息
    }
  }
  return signStorage
}

export default fetchSign